<?php
namespace App\Models\UserModels;
use App\Models\ParentModel;
use DB;
use Log;
/**
 * 抖音授权信息model
 * @author puppet
 */
class DyAccessToken extends ParentModel{
  /**
   * 与模型关联的数据表。
   *
   * @var string
   */
  protected $table = 'pt_dy_access_token';

  /**
   * DB::直接操作的数据库表名
   * @var string
   */
  protected $db_table = 'pt_dy_access_token';

  /**
   * 主键 默认id。
   *
   * @var string
   */
  protected $primaryKey = 'id';

  /**
   * 指定是否模型应该被戳记时间。
   *
   * @var bool
   */
  public $timestamps = false;
  
  /**
   * {"data":{"access_token":"act.00a50b6c4d426f891fc7a53e88738c45ELwlY1wLxa0fB6vr514x6eIyLZiR","captcha":"","desc_url":"","description":"","error_code":0,"expires_in":1296000,"open_id":"3562f0ac-57a8-46a2-b180-aa99f17be44e","refresh_expires_in":2592000,"refresh_token":"rft.764465d558b3ff1f5d4bb8e923185783DhMV40ejWmRLh7g6GohAljitUU85","scope":"user_info,video.search"},"message":"success"}
   */
  public function dataSave($access_token_info,$partner_id){
    $atData = $this->db_get(['partner_id'=>['=',$partner_id]]);
    $data = [];
    $data['access_token'] = $access_token_info['data']['access_token'];
    $data['description'] = $access_token_info['data']['description'];
    $data['error_code'] = $access_token_info['data']['error_code'];
    $data['expires_in'] = $access_token_info['data']['expires_in'];
    $data['open_id'] = $access_token_info['data']['open_id'];
    $data['refresh_expires_in'] = $access_token_info['data']['refresh_expires_in'];
    $data['refresh_token'] = $access_token_info['data']['refresh_token'];
    $data['scope'] = $access_token_info['data']['scope'];
    $nowTime = date('Y-m-d H:i:s');
    $data['update_time'] = $nowTime;
    if(!empty($atData)){
      $data['status'] = 1;
      $this->db_update('',$atData['id'],$data);
    }else{
      $data['partner_id'] = $partner_id;
      $data['create_time'] = $nowTime;
      $this->db_insert($data);
    }
    //更新用户信息
    $oUser = new \App\Models\UserModels\User();
    $userData = $oUser->db_get(['partner_id'=>['=',$partner_id]]);
    //获取到用户信息
    $userinfo = \App\Components\DyTools::userinfo($access_token_info['data']['open_id'],$access_token_info['data']['access_token']);
    Log::info('userinfo='.json_encode($userinfo));
    if($userinfo['error_code'] == 0){
      $uData = [];
      $uData['avatar'] = $userinfo['avatar'];
      $uData['e_account_role'] = $userinfo['e_account_role'];
      $uData['city'] = $userinfo['city'];
      $uData['gender'] = $userinfo['gender'];
      $uData['country'] = $userinfo['country'];
      $uData['union_id'] = $userinfo['union_id'];
      $uData['province'] = $userinfo['province'];
      $uData['nickname'] = $userinfo['nickname'];
      $uData['description'] = $userinfo['description'];
      $uData['error_code'] = $userinfo['error_code'];
      $uData['open_id'] = $access_token_info['data']['open_id'];
      $uData['update_time'] = $nowTime;
      if(!empty($userData)){
        $oUser->db_update('',$userData['id'],$uData);
      }else{
        $uData['partner_id'] = $partner_id;
        $uData['create_time'] = $nowTime;
        $oUser->db_insert($uData);
      }
    }else{
      $uData = [];
      $uData['open_id'] = $access_token_info['data']['open_id'];
      $uData['update_time'] = $nowTime;
      if(!empty($userData)){
        $oUser->db_update('',$userData['id'],$uData);
      }else{
        $uData['partner_id'] = $partner_id;
        $uData['union_id'] = '';
        $uData['create_time'] = $nowTime;
        $oUser->db_insert($uData);
      }
    }
  }
}